<template>
  <view class="tab-bar">
    <view class="tab-item" @click="switchTab(0)">
      <image :src="currentTab === 0 ? '/static/a3.png' : '/static/a3.png'" mode="aspectFit" class="tab-icon"></image>
      <text :class="['tab-text', currentTab === 0 ? 'active' : '']">首页</text>
    </view>
    <view class="tab-item" @click="switchTab(1)">
      <image :src="currentTab === 1 ? '/static/a4.png' : '/static/a4.png'" mode="aspectFit" class="tab-icon"></image>
      <text :class="['tab-text', currentTab === 1 ? 'active' : '']">报警记录</text>
    </view>
    <view class="tab-item" @click="onAddClick">
     <image src="/static/a5.png" mode="aspectFit" class="tab-icon"></image>
    </view>
    <view class="tab-item" @click="switchTab(3)">
      <image :src="currentTab === 3 ? '/static/a6.png' : '/static/a6.png'" mode="aspectFit" class="tab-icon"></image>
      <text :class="['tab-text', currentTab === 3 ? 'active' : '']">设备地图</text>
    </view>
    <view class="tab-item" @click="switchTab(4)">
      <image :src="currentTab === 4 ? '/static/a7.png' : '/static/a7.png'" mode="aspectFit" class="tab-icon"></image>
      <text :class="['tab-text', currentTab === 4 ? 'active' : '']">我的</text>
    </view>
  </view>
</template>

<script>
export default {
  name: 'TabBar',
  props: {
    current: {
      type: Number,
      default: 2
    }
  },
  data() {
    return {
      currentTab: this.current
    }
  },
  created() {
    // 初始化时触发页面切换
    this.switchTab(this.currentTab);
  },
  watch: {
    current(newVal) {
      this.currentTab = newVal;
    }
  },
  methods: {
    switchTab(index) {
      if (this.currentTab === index) return;
      this.currentTab = index;
      
      const pages = ['pages/index/index', 'pages/alarm/index', '', 'pages/map/index', 'pages/my/index'];
      if (pages[index]) {
        uni.switchTab({
          url: '/' + pages[index]
        });
      }
      this.$emit('tabChange', index);
    },
    onAddClick() {
      this.$emit('addClick');
    }
  }
}
</script>

<style lang="scss">
.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 100rpx;
  background-color: #ffffff;
  display: flex;
  box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
  z-index: 999;
  
  .tab-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding-top: 10rpx;
  }
  
  .tab-icon {
    width: 44rpx;
    height: 44rpx;
  }
  
  .tab-text {
    font-size: 24rpx;
    color: #999;
    margin-top: 4rpx;
    
    &.active {
      color: #2979ff;
    }
  }
  
  .tab-center {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100rpx;
    
    .tab-center-btn {
      width: 80rpx;
      height: 80rpx;
      background-color: #2979ff;
      border-radius: 50%;
      display: flex;
      justify-content: center;
      align-items: center;
      margin-bottom: 30rpx;
    }
    
    .tab-center-icon {
      font-size: 40rpx;
      color: #fff;
    }
  }
}
</style> 